﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.Common;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.BLL.Sys;
using Lenovo.CFI.BLL.DicMgr;
using Lenovo.CFI.Common.Sys;

namespace Lenovo.CFI.Web.VP
{
    public partial class MyIdeaDetail : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int? id = UrlHelper.GetQueryStringToInt("id");
                Idea idea = null;
                if (id.HasValue) idea = new IdeaBl().GetIdeaById(id.Value);
                if (idea == null) ErrorHelper.RaiseNoValueError();

                Topic topic = new TopicBl().GetTopicByID(idea.TopicID);
                this.TxtTopic.Text = topic.Title;
                this.TxtTitle.Text = idea.Title;

                this.TxtType.Text = IdeaTypeDicMgr.GetByCode(idea.Type).Title;
                string relatedType = "";
                foreach (string type in idea.RelatedType.List)
                {
                    relatedType += (IdeaTypeDicMgr.GetByCode(type).Title + ";");
                }
                this.TxtRelatedType.Text = relatedType;
                string keywords = "";
                foreach (string keyword in idea.Keywords.List)
                {
                    keywords += (KeywordDicMgr.GetByCode(keyword).Title + ";");
                }
                if (!String.IsNullOrEmpty(idea.FreewordsText))
                    keywords += (idea.FreewordsText + ";");
                this.TxtKeywords.Text = keywords;

                this.TxtBackground.Text = idea.Backgroud.Format4Html();
                this.TxtDescription.Text = idea.Description.Format4Html();
                this.TxtValue.Text = idea.Values.Format4Html();

                AttachmentBl attachmentBl = new AttachmentBl();
                if (idea.PptFileID.HasValue)
                    this.TxtPpt.Text = String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>",
                        AttachmentUrl.GetFileUrl(idea.PptFileID.Value, false), attachmentBl.GetAttachmentByID(idea.PptFileID.Value).Title);

                string pictures = "";
                if (idea.Pic1FileID.HasValue)
                    pictures += String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>&nbsp;&nbsp;",
                        AttachmentUrl.GetFileUrl(idea.Pic1FileID.Value, false), attachmentBl.GetAttachmentByID(idea.Pic1FileID.Value).Title);
                if (idea.Pic2FileID.HasValue)
                    pictures += String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>&nbsp;&nbsp;",
                        AttachmentUrl.GetFileUrl(idea.Pic2FileID.Value, false), attachmentBl.GetAttachmentByID(idea.Pic2FileID.Value).Title);
                if (idea.Pic3FileID.HasValue)
                    pictures += String.Format(@"<a href=""{0}"" target=""_blank"">{1}</a>&nbsp;&nbsp;",
                        AttachmentUrl.GetFileUrl(idea.Pic3FileID.Value, false), attachmentBl.GetAttachmentByID(idea.Pic3FileID.Value).Title);
                this.TxtPictures.Text = pictures;

                this.TxtOwner.Text = idea.Owner;
                this.TxtApprover.Text = idea.Approver;
                this.TxtStatus.Text = idea.StatusText;

                this.TxtAward.Text = idea.FinalAward.HasValue ? idea.FinalAward.Value.ToString() : "";

                if (idea.Approver != null)
                    this.TxtApproverComment.Text = (idea.Status == IdeaStatus.Reject ? "Reject<br/>" : "Agree<br/>") + (idea.ApproverComment.Format4Html());

                this.TxtDCComment.Text = idea.DcAttendMeeting.HasValue ? (idea.DcAttendMeeting.Value ? "Suggest to attend meeting<br/>" : "Suggest not to attend meeting<br/>") : "" + 
                    idea.DcComment.Format4Html();

                IdeaReview review = null;
                if (idea.FinalReviewID.HasValue)
                {
                    review = new ReviewBl().GetReviewByID(idea.FinalReviewID.Value);

                    this.TxtTime.Text = String.Format("{0:yyyy-MM-dd HH:mm}&nbsp;&nbsp;{1} min", review.ReviewTime.Value, review.ReviewPeriod);

                    if (idea.Status > IdeaStatus.Suggestion)
                    {
                        this.TxtSummary.Text = review.ReviewSummary;
                        this.TxtScore.Text = review.ReviewScore.ToString();
                        this.TxtFinalType.Text = IdeaTypeDicMgr.GetByCode(review.ReviewType).Title;

                        this.TxtNextAction.Text = NextActionDicMgr.GetByCode(review.NextAction).Title;
                        this.TxtIP.Text = review.NextIP.HasValue ? (review.NextIP.Value ? "Y" : "N") : "";

                        this.TxtActionExecutor.Text = review.ActionExecutor;
                        this.TxtActionFeedback.Text = String.Format("{0}<br/>{1}<br/>{2:yyyy-MM-dd}<br/>{3}", review.ActionStatusText, 
                            review.ProjectName, review.ProjectTime, review.ActionComment);

                        this.TxtIPExecutor.Text = review.IpExecutor;
                        this.TxtIPFeedback.Text = String.Format("{0}<br/>{1}<br/>{2}", review.IpStatusText,
                            review.IpNo, review.IpComment);
                    }
                }

                #region 验证权限
                // owner
                // manager vp
                // assistant
                // admin
                // dc ip
                // reviewer
                // executor
                // level or list

                bool canRead = false;
                if (idea.Owner == UserHelper.UserName)
                    canRead = true;
                if (!canRead)
                {
                    User user = new UserBl().GetUserByItCode(UserHelper.UserName);
                    if (user.Manager == user.ItCode || user.VP == user.ItCode)
                        canRead = true;
                }
                if (!canRead)
                {
                    if (idea.Approver == UserHelper.UserName)
                        canRead = true;
                }
                if (!canRead)
                {
                    if (UserHelper.HasAccessPerm(UserRole.ROLE_ASSISTANT1.ToString()) || 
                        UserHelper.HasAccessPerm(UserRole.ROLE_ASSISTANT2.ToString()) || 
                        UserHelper.HasAccessPerm(UserRole.ROLE_ADMIN.ToString()))
                        canRead = true;
                }
                if (!canRead)
                {
                    if (idea.SecretLevel <= UserHelper.PermLevel)
                        canRead = true;
                }
                if (!canRead)
                {
                    // 不够严谨
                    if (UserHelper.HasAccessPerm(UserRole.ROLE_DC.ToString()) ||
                        UserHelper.HasAccessPerm(UserRole.ROLE_IP.ToString()) ||
                        UserHelper.HasAccessPerm(UserRole.ROLE_REVIEWER.ToString()))
                        canRead = true;
                }
                if (!canRead)
                {
                    if (review != null && (review.ActionExecutor == UserHelper.UserName ||
                        review.IpExecutor == UserHelper.UserName))
                        canRead = true;
                }
                if (!canRead)
                {
                    if (new UserIdeaAppBl().CanAccess(UserHelper.UserName, idea.ID))
                        canRead = true;
                }


                if (!canRead)
                {
                    if (UrlHelper.GetQueryString("a") == "1")
                        Server.Transfer(UrlHelper.GenerateUrl("ideaapply", "id", idea.ID.ToString()));
                    else
                        ErrorHelper.RaiseNoPermError();
                }

                #endregion
            }
        }
    }
}